package myJs.api

import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import sttp.client3._
import sttp.model._
import upickle.default._

import scala.concurrent.Future

/**
 * Created by yz on 7/11/2022
 */
object PredictApi {

  import shared.pojo.Pojo.predict._

  val baseUrl = s"${Api.apiUrl}/liveForest/user/predict"

  def predict(request: PredictData) = {
    val url = s"${baseUrl}/predict"
    Api.preparseRequest[Predict.Response]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def predictResult(request: PredictResult.Request) = {
    val url = s"${baseUrl}/predictResult"
    Api.preparseRequest[PredictResult.Response]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

}
